module.exports = (EdtApi) => {
    "use strict";
    let chatId = "", count = 0, pl;
    function isGroup() {
        return false;
    }
    async function getChatId(times = 0) {
        let idEl = document.querySelector('div.ChatBlockHeader_title__bB_ff');
        let id = idEl?.textContent || "";
        if (id && id != chatId) return id;
        await EdtApi.sleep(50);
        if (times > 10) return id;
        times++;
        return getChatId(times);
    }
    return {
        config: {
            friendList: 'main>div.phound-Flex-root>div:nth-child(1) div[data-testid="virtuoso-item-list"],main>div.phound-Flex-root>div:nth-child(1)',
            chat: 'div[data-view="main"]',
            input: 'div.phound-Dropzone-inner div[role="textbox"]',
            msgBox: '#chat-virtuoso div[data-testid="virtuoso-item-list"]',
            inMsg: 'div[data-index] div.ChatMessage_root__hBnyk:not(.ChatMessage_self__xkpx2) div.phound-Spoiler-content:not(.trans-inited)',
            outMsg: 'div[data-index] div.ChatMessage_root__hBnyk.ChatMessage_self__xkpx2 div.phound-Spoiler-content:not(.trans-inited)',
            allMsg: "div[data-index] div.ChatMessage_root__hBnyk div.phound-Spoiler-content",
            sendBtn: 'div.ChatBottomBlock_controls-for-new-message__uyZRp>div[role="presentation"] div.phound-RichTextEditor-root>button.RTEInput_send-btn__fa_yx',
            setting: 'div.phound-Dropzone-root[role="presentation"]',
            tips: {
                in: 'div.ChatBlockHeader_root__ffNJs div.ChatBlockHeader_chat-info__jqk8r',
                out: 'div.ChatBottomBlock_bottom-block__k0A1B'
            },
            crm: {
                // listener: '#LeftColumn-main',
                top: '.ChatBlockHeader_chat-info__jqk8r',
                left: ""
            },
            extra: {//其他会触发对话框变动的情况处理
                selector: 'a[aria-label="Messages"],button.phound-active.phound-UnstyledButton-root',
                level: 20,
                delay: 600,
            },
            autoCount: 20,
        },
        init(handler) {
            let btn;
            document.addEventListener('click', e => {
                btn = EdtApi.closest(e.target, this.config?.sendBtn, 20);
                if (btn) {
                    if (e.isTrusted && handler.preventSend(e)) {
                        e.preventDefault();
                        e.stopPropagation();
                        e.stopImmediatePropagation();
                        handler.sendClick(e);
                        handler.resetPlaceholder();
                    }
                }
            });
        },
        setPlaceholder({ tipMsgs }) {
            let plEl = document.querySelector('div.phound-RichTextEditor-content');
            if (!plEl) return;
            pl = plEl.querySelector("div.edt-placeholder");
            if (!pl) {
                pl = document.createElement("div");
                pl.className = 'edt-placeholder';
                plEl.append(pl);
            }
            pl.innerHTML = tipMsgs;
        },
        getUnread() {
            count = 0;
            const notifyEls = document.querySelectorAll('main>div.phound-Flex-root>div:nth-child(1) div[data-testid="virtuoso-item-list"] div.NotificationBadge_root__eIDPw');
            notifyEls?.forEach((el) => {
                count += EdtApi.safeParseInt(el.textContent || "0");
            });
            return count;
        },
        getChatInfo() {
            return new Promise(async (resolve) => {
                chatId = await getChatId();
                resolve({ id: chatId, isGroup: isGroup() });
            });
        },
        getText(msgBox) {
            return msgBox?.textContent || "";
        },
        getIptMsg(inputEl) {
            return inputEl?.textContent || "";
        },
        getUserInfo() {
            try {
                let userEl = document.querySelector('p.UserAvatar_initials__2S3n5.phound-Text-root');
                let phoneEl = document.querySelector('div.PersonasSelector_group__sifMH div.PhoneWithFlag_number__8qlX2');
                let userName = userEl?.textContent || "";
                let avatar = "";
                let account = phoneEl?.textContent || "";
                return {
                    avatar,
                    userName,
                    account
                };
            } catch (error) {
                return null;
            }
        }
    }
};